home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / netclb23.zip / EXAMPLES.EXE / FLIST.C < prev    next >
C/C++ Source or Header  |  1994-05-20  |  10KB  |  302 lines

  1. /***************************************************************************/
  2. /* File:             FLIST.C - Copyright (C) Adrian M. Cunnelly 1993       */
  3. /*                                                                         */
  4. /* Function:         For all connected workstations list all files that    */
  5. /*                   are open on the current server.                       */
  6. /*                                                                         */
  7. /*                   This has been tested with Netware 2.15 and 3.11       */
  8. /*                                                                         */
  9. /* Usage:            flist                                                 */
  10. /*                                                                         */
  11. /* Functions Called: CheckConsolePrivileges                                */
  12. /*                   GetBinderyObjectID                                    */
  13. /*                   GetConnectionInformation                              */
  14. /*                   GetConnectionNumber                                   */
  15. /*                   GetConnectionsOpenFiles                               */
  16. /*                   GetFileServerInformation                              */
  17. /*                   GetObjectConnectionNumbers                            */
  18. /*                   GetPathFromDirectoryEntry                             */
  19. /*                   GetPreferredConnectionID                              */
  20. /*                   GetDefaultConnectionID                                */
  21. /*                   GetPrimaryConnectionID                                */
  22. /*                   SetPreferredConnectionID                              */
  23. /*                   ISShellLoaded                                         */
  24. /*                                                                         */
  25. /***************************************************************************/
  26.  
  27. #include <sys\types.h>
  28. #include <sys\stat.h>
  29. #include <io.h>
  30. #include <errno.h>
  31. #include <conio.h>
  32. #include <dos.h>
  33.  
  34. #ifndef TURBOC
  35. #include <search.h>
  36. #endif
  37.  
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <time.h>
  42. #include <fcntl.h>
  43. #include <ctype.h>
  44.  
  45. #include "netware.h"
  46.  
  47. void show_usage( void );
  48. int get_max_connections( void );
  49. void do_connection( int station );
  50. int do_it( int argc, char *argv[]);
  51.  
  52. int this_con,max_con;
  53.  
  54. int main(int argc,char *argv[])
  55. {
  56. int thisserver,prefserver;
  57. int rcode;
  58.  
  59.    printf("\nFLIST (v2.0) - Copyright (C) Adrian M. Cunnelly 1993\n\n");
  60.  
  61.    if (IsShellLoaded() != SUCCESS)
  62.    {
  63.       printf("*** No netware shell loaded ***\n");
  64.       exit(255);
  65.    }
  66.  
  67.    if ((prefserver = GetPreferredConnectionID()) == 0)
  68.    {
  69.       if ((thisserver = GetDefaultConnectionID()) == 0)
  70.          thisserver = GetPrimaryConnectionID();
  71.       SetPreferredConnectionID( thisserver );
  72.    }
  73.    else
  74.       thisserver = prefserver;
  75.  
  76.    if ((this_con = GetConnectionNumber()) == 0)
  77.    {
  78.      printf("*** No current connection ***\n");
  79.      if (thisserver != prefserver)   /* reset preferred server */
  80.         SetPreferredConnectionID( prefserver );
  81.      exit(255);
  82.    }
  83.    if (CheckConsolePrivileges() != 0)
  84.    {
  85.       printf("FLIST requires user to have Console Operator rights");
  86.       printf("... aborted\n");
  87.       if (thisserver != prefserver)   /* reset preferred server */
  88.          SetPreferredConnectionID( prefserver );
  89.       exit(255);
  90.    }
  91.    if ((max_con = get_max_connections()) == 0)
  92.    {
  93.       if (thisserver != prefserver)   /* reset preferred server */
  94.          SetPreferredConnectionID( prefserver );
  95.       exit(255);
  96.    }
  97.  
  98.    rcode = do_it( argc, argv );
  99.    if (thisserver != prefserver)   /* reset preferred server */
  100.        SetPreferredConnectionID( prefserver );
  101.    return( rcode );
  102. }
  103.  
  104. int do_it( int argc, char *argv[])
  105. {
  106. unsigned int    station;
  107. char *this_param;
  108. int param=1;
  109. int req_connection = -1;
  110. long req_user = 0L;
  111. char req_uname[OBJECT_LENGTH];
  112. word conn_count;
  113. byte connections[100];
  114. int r;
  115.  
  116.    while(--argc)
  117.    {
  118.       this_param = argv[param++];
  119.  
  120.       if(*this_param == '-' || *this_param == '/')
  121.       {
  122.          switch(toupper(*(++this_param)))
  123.          {
  124.              case 'C':
  125.                       if (req_user > 0L)
  126.                       {
  127.                          printf("Connection number cannot be specified if");
  128.                          printf(" username is used\n\n");
  129.                          show_usage();
  130.                          return(255);
  131.                       }
  132.                       if ((req_connection = atoi(this_param+1)) < 1)
  133.                       {
  134.                          printf("Connection number [%d] is invalid\n\n",
  135.                                 req_connection);
  136.                          show_usage();
  137.                          return(255);
  138.                       }
  139.                       break;
  140.              case 'U':
  141.                       if (req_connection > 0)
  142.                       {
  143.                          printf("Username cannot be specified if connection");
  144.                          printf(" number is used\n\n");
  145.                          show_usage();
  146.                          return(255);
  147.                       }
  148.                       if ( GetBinderyObjectID( USER ,
  149.                                                this_param+1 ,
  150.                                                &req_user ) != SUCCESS)
  151.                       {
  152.                          printf("Invalid username specified: %s\n\n",
  153.                                 this_param+1);
  154.                          show_usage();
  155.                          return(255);
  156.                       }
  157.                       else
  158.                          strcpy(req_uname,this_param+1);
  159.                       break;
  160.              case '?':
  161.                       show_usage();
  162.                       return(0);
  163.                       break;
  164.              default:
  165.                       printf("Invalid option specified\n\n");
  166.                       show_usage();
  167.                       return(255);
  168.          }
  169.       }
  170.       else
  171.       {
  172.          printf("Invalid parameter specified\n\n");
  173.          show_usage();
  174.          return(255);
  175.       }
  176.    }
  177.  
  178.    if (req_connection > max_con)
  179.    {
  180.       printf("Connection number [%d] out of range, max allowed is %d\n",
  181.              req_connection,max_con);
  182.       return(255);
  183.    }
  184.  
  185.    if (req_connection > 0)
  186.          do_connection(req_connection);
  187.    else
  188.    if (req_user > 0L)
  189.    {
  190.       if ( ( r = GetObjectConnectionNumbers( USER , req_uname , &conn_count ,
  191.                                              connections ) ) != SUCCESS )
  192.       {
  193.          printf("GetObjectConnectionNumbers returned:  %03d--%#02x\n",
  194.                 r,r);
  195.          return(255);
  196.       }
  197.       for (station=0; station < conn_count; station++)
  198.           do_connection( connections[ station ] );
  199.    }
  200.    else
  201.       for (station=1; station<=max_con; station++)
  202.           do_connection(station);
  203.  
  204.    return(0);
  205. }
  206.  
  207. int get_max_connections( void )
  208. {
  209. FILE_SERVER_INFO sinfo;
  210. int r;
  211.  
  212.     if ((r=GetFileServerInformation( &sinfo )) != SUCCESS)
  213.     {
  214.        printf("GetFileServerInformation returned:  %03d--%#02x\n",r,r);
  215.        return (0);
  216.     }
  217.  
  218.     return( convertNWint(sinfo.connections_supported) );
  219. }
  220.  
  221. void do_connection( int station )
  222. {
  223. int fcount=0;
  224. long object_id;
  225. word object_type;
  226. char object_name[OBJECT_LENGTH];
  227. byte logintime[7];
  228. char fname[14];
  229. int r,r1;
  230. int sequence;
  231. byte lock,access,locktype,volume,namespace;
  232. word task;
  233. dword dirent,parentdirent;
  234. char path[256];
  235.  
  236.   if((r1=GetConnectionInformation( station , object_name,
  237.                                    &object_type,&object_id,
  238.                                    logintime)) != SUCCESS)
  239.   {
  240.     printf("GetConnectionInformation returned:  %03d--%#02x\n",r,r);
  241.     return;
  242.   }
  243.   else
  244.   if ( (object_id == 0) || (object_name[0] == '\0') )
  245.      return;
  246.   else
  247.   {
  248.      if (station != this_con)
  249.         printf("   %2u-%-16s\n",station , object_name );
  250.      else
  251.         printf(" \376 %2u-%-16s\n",station , object_name );
  252.  
  253.      sequence=0;
  254.  
  255.      do
  256.      {
  257.        if ((r=GetConnectionsOpenFiles(station,&sequence,&task,&lock,
  258.                                       &access,&locktype,&namespace,
  259.                                       &volume,&parentdirent,
  260.                                       &dirent,fname)) != SUCCESS)
  261.           printf("GetConnectionsOpenFiles returned:  %03d--%#02x\n",
  262.                  r,r);
  263.        else
  264.        if (sequence != -1)
  265.        {
  266.           if (dirent != 0)
  267.           {                     /* Must be on Netware 3.11 */
  268.              if ((r1=GetPathFromDirectoryEntry(namespace,volume,
  269.                                                dirent,path)) == SUCCESS)
  270.                 printf("       %s\n",path);
  271.           }
  272.           else
  273.           {                     /* Must be Netware 2.15 */
  274.              if ((r1=GetPathFromDirectoryEntry(namespace,volume,
  275.                                                parentdirent,path)) == SUCCESS)
  276.                 printf("       %s/%s\n",path,fname);
  277.           }
  278.           if (r1 != SUCCESS)
  279.              printf("GetPathFromDirectoryEntry returned:  %03d--%#02x\n",
  280.                     r1,r1);
  281.           else
  282.              fcount++;
  283.        }
  284.      } while ((r == SUCCESS) && (sequence != -1));
  285.  
  286.      if (fcount == 0)
  287.         printf("       ** No Open Files **\n");
  288.   }
  289. }
  290.  
  291. void show_usage( void )
  292. {
  293.    printf("Usage: Flist <options>\n\n");
  294.    printf("   Options:\n");
  295.    printf("            -cn  - Show open files for connection number 'n'\n");
  296.    printf("                                [ default: all connections ]\n");
  297.    printf("            -uxx - Show open files for all connections logged in");
  298.    printf(" as user xx\n");
  299.    printf("            -?   - This screen of information\n");
  300.    printf("\n");
  301. }
  302.